package com.starrtc.demo.demo.voip;

import android.annotation.SuppressLint;

import com.starrtc.demo.demo.MLOC;
import com.starrtc.starrtcsdk.core.pusher.EncodeShareObject;
import com.starrtc.starrtcsdk.core.pusher.XHCameraRecorder;
import com.starrtc.starrtcsdk.core.pusher.XHCustomRecorder;

import java.lang.reflect.Field;
import java.util.Timer;
import java.util.TimerTask;

public class PushUVCTest {

    private XHCameraRecorder recorder;
    private int fps = 20;
    public byte[] frameData;

    EncodeShareObject a;

    public PushUVCTest(XHCameraRecorder recorder){
        this.recorder = recorder;
    }

    public void startRecoder(){
        //启动XHCustomRecorder
        recorder.start();

        Field field = null;
        try {
            field = recorder.getClass().getDeclaredField("shareObject");
            field.setAccessible(true);
            a = (EncodeShareObject) field.get(recorder);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }



        //初始化Camera，持续采集视频，把采集到的数据传给sdk
        uploadTask();
    }

    public void stopRecoder(){
        //停止XHCustomRecorder
        recorder.stop();

        //停止上传任务
        if(uploadTimer!=null){
            uploadTimer.cancel();
            uploadTimer = null;
        }

        //销毁Camera
    }

    private Timer uploadTimer;
    private void uploadTask(){
        if(uploadTimer!=null){
            uploadTimer.cancel();
            uploadTimer = null;
            uploadTimer = new Timer(false);
        }else{
            uploadTimer = new Timer(false);
        }
        uploadTimer.schedule(new TimerTask() {
            @SuppressLint({"NewApi", "LocalSuppress"})
            @Override
            public void run() {
                uploadVideoData();
            }
        },0,1000/fps);
    }

    @SuppressLint({"NewApi", "LocalSuppress"})
    private void uploadVideoData(){
        MLOC.d("!!!!!!!!!!!!!","获取数据为：sdfsdfsd");
        // 获取TestClass对象的所有变量
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.a());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.b());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.c());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.d());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.e());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.f());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.g());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.h());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.i());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.j());
        MLOC.d("!!!!!!!!!!!!!","获取数据为：" +a.k());



       /* if(recorder.rotationDataNV12 != null){
            MLOC.d("!!!!!!!!!!!!!","获取数据为：" +recorder.rotationDataNV12.toString());
        }
        if(recorder.rotationDataNV12Small != null){
            MLOC.d("!!!!!!!!!!!!!","获取数据为：" +recorder.rotationDataNV12Small.toString());
        }*/
//        recorder.fillData_NV21(frameData);
    }
}
